package com.lzxuni.controller;

import com.github.pagehelper.PageInfo;
import com.lzxuni.po.*;
import com.lzxuni.service.RhDataService;
import com.lzxuni.service.SheBeiService;
import com.lzxuni.service.YongHuService;
import com.lzxuni.utils.Chongzushuju;
import com.lzxuni.utils.JsonResultUtil;
import com.lzxuni.utils.MethodUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Iterator;
import java.util.List;


@Controller
@RequestMapping("/shebei")
public class SheBeiController {
	@Autowired
	private RhDataService rhDataService;
	@Autowired
	private YongHuService yongHuService;
	@Autowired
	private SheBeiService sheBeiService;


	@RequestMapping("/list.html")
	public ModelAndView shebeilist() {
		ModelAndView mv = new ModelAndView("/shebei/list");
		return mv;
	}

	@RequestMapping("/listDo.html")
	public void listshebeiDo(QxDataCostom qxDataCustom, HttpServletRequest request, HttpServletResponse response) throws Exception {

		int pageNumber = request.getParameter("pageNumber") == null ? 1 : Integer.parseInt(request.getParameter("pageNumber"));
		int pageSize = request.getParameter("pageSize") == null ? 10 : Integer.parseInt(request
				.getParameter("pageSize"));

		// 分页
		@SuppressWarnings("rawtypes")
		PageInfo pageInfo = sheBeiService.findShebeiList(pageNumber, pageSize, new SheBei());

		@SuppressWarnings("unchecked")
		List<SheBei> sheBeiList = pageInfo.getList();
		// 返回json数组分页
		JSONObject jsonObject = new JSONObject();
		JSONArray array = new JSONArray();

		for (Iterator<SheBei> iterator = sheBeiList.iterator(); iterator.hasNext(); ) {
			JSONObject jsonObject1 = new JSONObject();
			SheBei shebei = iterator.next();
			//检查是否为userkey创建名称
			//YongHu yongHu = yongHuService.findYongHuById(qxData.getUserkey());
//            if (yongHu == null) {
//                YongHu yongHu2 = new YongHu();
//                yongHu2.setUserkey_id(qxData.getUserkey());
//                yongHu2.setCreateTime(MethodUtil.getDate(1, null));
//                yongHuService.insertYongHu(yongHu2);
//                yongHu = yongHuService.findYongHuById(qxData.getUserkey());
//            }
			jsonObject1.put("userkey", shebei.getUserkey());
			jsonObject1.put("userName", shebei.getUserName());
			jsonObject1.put("lastTime", MethodUtil.formatDate(3, shebei.getLasttime()));
			array.add(jsonObject1);
		}
		jsonObject.put("total", pageInfo.getTotal());
		jsonObject.put("rows", array);

		// 输出
		MethodUtil.writer(response, jsonObject.toString());


	}

	// 跳转设备数据编辑页面
	@RequestMapping("update.html")
	public ModelAndView update(String userkey) throws Exception {
		SheBei shebei = sheBeiService.findSheBeiById(userkey);
		ModelAndView mv = new ModelAndView("/shebei/update");
		mv.addObject("shebei", shebei);
		return mv;
	}

	// 设备数据编辑
	@ResponseBody
	@RequestMapping("updateDo.html")
	public Object updateDo(SheBei shebei) throws Exception {
		sheBeiService.updateSheBeiuserName(shebei);
		return JsonResultUtil.getSuccessJson("成功！");
	}


	// 跳转设备数据  查看全部数据
	@RequestMapping("checkall.html")
	public ModelAndView check() throws Exception {
		ModelAndView mv = new ModelAndView("/shebei/checkall");
		return mv;

	}


	// 设备数据  查看全部数据
	@RequestMapping("/checkallDo.html")
	public void checkallDo(QxDataCostom qxDataCustom, HttpServletRequest request, HttpServletResponse response) throws Exception {

		int pageNumber = request.getParameter("pageNumber") == null ? 1 : Integer.parseInt(request.getParameter("pageNumber"));
		int pageSize = request.getParameter("pageSize") == null ? 10 : Integer.parseInt(request
				.getParameter("pageSize"));

		String userkey = request.getParameter("userkey");
		System.out.println("userkey----------" + userkey);
		String tableName = "tbl_" + userkey;

		// 分页
		@SuppressWarnings("rawtypes")
		PageInfo pageInfo = sheBeiService.findqxsjBytableName(pageNumber, pageSize, tableName);
		@SuppressWarnings("unchecked")
		List<QxData> qxDataList = pageInfo.getList();
		// 返回json数组分页
		JSONObject jsonObject = new JSONObject();
		JSONArray array = new JSONArray();

		for (Iterator<QxData> iterator = qxDataList.iterator(); iterator.hasNext(); ) {
			JSONObject jsonObject1 = new JSONObject();
			QxData qxData = iterator.next();

			jsonObject1.put("id", qxData.getId());
			jsonObject1.put("userkey", qxData.getUserkey());
			jsonObject1.put("createTime", MethodUtil.formatDate(0, qxData.getCreateTime()));
			jsonObject1.put("kqwd", qxData.getKqwd());
			jsonObject1.put("kqsd", qxData.getKqsd());
			jsonObject1.put("gz", qxData.getGz());
			jsonObject1.put("trwd", qxData.getTrwd());
			jsonObject1.put("trsd", qxData.getTrsd());
			jsonObject1.put("fs", qxData.getFs());
			jsonObject1.put("fx", qxData.getFx());
			jsonObject1.put("eyht", qxData.getEyht());
			jsonObject1.put("yl", qxData.getYl());

			array.add(jsonObject1);
		}
		jsonObject.put("total", pageInfo.getTotal());
		jsonObject.put("rows", array);

		// 输出
		MethodUtil.writer(response, jsonObject.toString());


	}


	// 设备数据  查看全部数据
	@RequestMapping("/ceshi.html")
	public Object shuju123(HttpServletRequest request) throws Exception {
		String userkey = request.getParameter("userkey");
		String data = request.getParameter("data");

		String newdata=Chongzushuju.NewString(userkey,data);
		System.out.println(newdata);
		String[] dataArr= newdata.split(",");
		for(int i=0;i<dataArr.length;i++){
			System.out.println(i+"---------"+dataArr[i]);
		}

		return JsonResultUtil.getSuccessJson("成功！");

	}
	@RequestMapping("/toceshi.html")
	public ModelAndView toceshi(String userkey) throws Exception {
		ModelAndView mv = new ModelAndView("/shebei/ceshi");
		return mv;
	}


}